// pages/hotdan/hotdan.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:{},
    isHideLoadMore:false,
    page: 1,
    key: '',
    num:1,
    producttypeid: null,
    showTip: false,
    hotdans: [],
    title:'',
    img: app.globalData.img
  },

  showTip: function (e) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(function () {
      var animation = wx.createAnimation({
        duration: 1000,
        timingFunction: 'ease'
      });
      animation.translate(0, -220).step()
      that.setData({
        showTip: true,
        index: that.data.hotdans[e.target.dataset.index]
      })
      setTimeout(function () {
        that.setData({
          ani: animation.export()
        })
      }, 1)
      wx.hideLoading()
    }, 1000)
  },
  closeTip: function (e) {
    var animation = wx.createAnimation({
      duration: 1000,
      timingFunction: 'ease'
    });
    animation.translate(0, 0).step()
    if (e.target.id) {
      this.setData({
        showTip: false,
        ani: animation.export()
      })
    }

  },
  // 购物车减加
  sub:function() {
    if (this.data.num <=1) {
      this.setData({
        num:1
      })
    } else {
      this.setData({
        num:this.data.num-1
      })
    }
  },
  add:function() {
    this.setData({
      num:this.data.num+1
    })
  },
  // 点击确定加入购物车 弹框
    pushcard: function (e) {
      var animation = wx.createAnimation({
        duration: 1000,
        timingFunction: 'ease'
      });
      animation.translate(0, 0).step()
      if (e.target.id) {
        this.setData({
          showTip: false,
          ani: animation.export()
        })
      }
      wx.showToast({
        title: '添加成功',
        icon: 'success'
      })
      this.setData({
        num:1
      })
  },

  getList:function() {
    var that = this
    app.req({
      url: 'Product/Type/Product/' + this.data.producttypeid,
      data: {
        pageindex: this.data.page,
        pagesize: 15,
      },
      success: function (res) {
        if (that.data.page == 1) {
          that.setData({
            hotdans: res.data.Result
          })
        } else if (that.data.page > 1) {
          that.setData({
            hotdans: that.data.hotdans.concat(res.data.Result)
          })
        }
        // } 
        if (res.data.Result.length == 0) {
          that.setData({
            page: that.data.page - 1
          })
        }
        setTimeout(function () {
          wx.stopPullDownRefresh()
        }, 1000)


        // that.setData({
        //   hotdans: res.data.Result
        // })


      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: options.name
    })
    var that = this
    this.setData({
      producttypeid: options.id,
      title:options.name
    })

    this.getList()


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    this.getList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多')
    this.setData({
      isHideLoadMore: false,
    })
    this.setData({
      page: this.data.page + 1
    })
    this.getList()
    setTimeout(() => {
      this.setData({
        isHideLoadMore: true,
      })
    }, 1000)

  },




  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})